import axios from 'axios';
import { Loading } from 'element-ui'
const loading = {
    loadingInstance: null,
    open: function () {
        if (this.loadingInstance === null) {
            this.loadingInstance = Loading.service({
                text: '拼命加载中',
                target: '.main',//效果显示区域
                // background: 'rgba(0,0,0,.5)'// 加载效果
            })
        }
    },
    close: function () {
        if (this.loadingInstance !== null) {
            this.loadingInstance.close()
        }
        this.loadingInstance = null // 关闭后实例重新赋值null, 下次让它重新创建
    }
}
const request = axios.create({
    baseURL: '/dev-api',
    timeout: 5000
})
//请求拦截器  请求就拦截
request.interceptors.request.use(config=>{
    loading.open();
    return config
},error=>{//出现异常
    loading.close();
    return Promise.reject(error)
})
//响应拦截器  响应拦截
request.interceptors.response.use(response=>{
    loading.close();
    return response;
},error=>{//出现异常
    loading.close();
    return Promise.reject(error)
})
export default request;